import Config from './Config.js';
import $ from 'jquery';
import {
    message
} from 'antd';

const Action = {
    changeKey: function(data, state, newStateHandler){
        var game;
        switch (data.key) {
            case '0':
                this.getDouyu({game:'all'},state,newStateHandler);
                game = 'all';
                break;
            case '1':
                this.getXiongmao({game:'lol'},state,newStateHandler);
                game = 'lol';
                break;
            case '2':
                this.getZhanqi({game:'lol'},state,newStateHandler);
                game = 'lol';
                break;
            default:
        }
        newStateHandler({
            currentKey: data.key,
            currentGame: game
        })
    },
    getDouyu: function(data, state, newStateHandler){
        var url = Config.getApiPath("getDouyu");
        var requestData = {
            game: data.game
        };
        if(!state.loading){
            newStateHandler({
                currentGame: data.game,
                loading: true
            })
        }
        $.post(url, requestData, function(res){
            try {
                res = JSON.parse(res);
            } catch (e) {
                message.error('获取直播列表失败');
                return;
            }
            if(res.error == 0){
                newStateHandler({
                    loading: false,
                    lives: res.data
                })
            }else{
                message.info('获取直播列表失败');
            }
        });
    },
    getZhanqi: function(data, state, newStateHandler){
        var url = Config.getApiPath("getZhanqi");
        var requestData = {
            game: data.game
        };
        if(!state.loading){
            newStateHandler({
                currentGame: data.game,
                loading: true
            })
        }
        $.post(url, requestData, function(res){
            try {
                res = JSON.parse(res);
            } catch (e) {
                message.error('获取直播列表失败');
                return;
            }
            if(res.error == 0){
                newStateHandler({
                    loading: false,
                    lives: res.data,
                })
            }else{
                message.info('获取直播列表失败');
            }
        });
    },
    getXiongmao: function(data, state, newStateHandler){
        var url = Config.getApiPath("getXiongmao");
        var requestData = {
            game: data.game
        };
        if(!state.loading){
            newStateHandler({
                currentGame: data.game,
                loading: true
            })
        }
        $.post(url, requestData, function(res){
            try {
                res = JSON.parse(res);
            } catch (e) {
                message.error('获取直播列表失败');
                return;
            }
            if(res.error == 0){
                newStateHandler({
                    loading: false,
                    lives: res.data
                })
            }else{
                message.info('获取直播列表失败');
            }
        });
    },
    changePage: function(data, state, newStateHandler){
        newStateHandler({
            currentPage: data.currentPage
        })
    },
    viewLive: function(data, state, newStateHandler){
        var {
            lives,
            currentKey,
            currentPage
        } = state;
        var currentIndex = (currentPage-1)*6 + data.index;
        var room_id = lives[currentIndex].room_id;
        var url = '';
        switch (currentKey) {
            case '0':
                url = "https://www.douyu.com/"+room_id;
                break;
            case '1':
                url = 'http://www.panda.tv'+room_id;
                break;
            case '2':
                url = 'https://www.zhanqi.tv'+room_id;
                break;
            default:
        }
        window.open(url);
    },
    imgError: function(data, state, newStateHandler){
        var {
            lives,
            currentKey,
            currentPage
        } = state;
        var currentIndex = (currentPage-1)*6 + data.index;
        var img_src = '';
        switch (currentKey) {
            case '0':
                img_src = 'https://shark.douyucdn.cn/app/douyu/res/page/list-item-def-thumb.gif'
                break;
            case '1':
                img_src = 'https://shark.douyucdn.cn/app/douyu/res/page/list-item-def-thumb.gif'
                break;
            case '2':
                img_src = 'https://shark.douyucdn.cn/app/douyu/res/page/list-item-def-thumb.gif'
                break;
            default:
        }
        lives[currentIndex].room_src = img_src;
        newStateHandler({
            lives
        })
    }
}
export default Action;
